<?php
/**
* @version		$Id: mod_slg_vm_productslider.php r5 2010-10-24 imranweb7 $
* @copyright	Copyright (C) 2010 imranweb7.com. All rights reserved.
* @license		GNU/GPL
* @Author:      Muhammed Imran Hussain
* @Websites:    http://www.imranweb7.com

* mod_slg_vm_productslider! is a free Joomla+Virtuemart module.
* This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/

// no direct access
defined('_JEXEC') or die('Restricted access');
if (! class_exists("modSliderHelper") ) { 

	class ModSliderHelper {
		
		/**
		 * Make query to virtuemart table and get category id and name by category ids
		 *
		 * @access	public
		 * @param	mixed	$catids virtuemart category ids
		 * @return	mixed   an array of database objects using the current SQL query or false if the query fails
		 */
		function getVmCategoryNameById($catids){
			$db = &JFactory::getDBO();
			
			if(is_array($catids)){
				$catids = implode(',', $catids);
			}
	
			$db->setQuery("SELECT `category_id`, `category_name` FROM #__vm_category WHERE category_id IN (".$catids.")");
			
			return $db->loadObjectList();
		}
		
		
		/**
		 * Make query to virtuemart table and get category id which has at least 1 prroduct
		 *
		 * @access	public
		 * @param	
		 * @return	mixed   an array of database objects using the current SQL query or false if the query fails
		 */
		function getVmCategoryNameByHasProduct(){
			$db = &JFactory::getDBO();
			
			$db->setQuery("SELECT `category_id` FROM `#__vm_product_category_xref` GROUP BY `category_id` ORDER BY `category_id`");	
			
			return self::getVmCategoryNameById($db->loadResultArray());
		}
		
		
		/**
		 * Make query to virtuemart table and get products by category id
		 *
		 * @access	public
		 * @param	integer $catid virtuemart category id
		 * @param	integer $item_limit query rows limit
		 * @param	string $type product type newest/feature etc.
		 * @param	string $s_desc product short description maximum allowed characters.
		 * @return	mixed   an array of database objects using the current SQL query or false if the query fails
		 */
		function getProductByCatId($catid, $item_limit=5, $type='newest', $s_desc=200){
			global $my, $mosConfig_absolute_path, $mosConfig_offset;
			
			// Load the virtuemart main parse code
			if( file_exists(dirname(__FILE__).'/../../components/com_virtuemart/virtuemart_parser.php' )) {
				require_once( dirname(__FILE__).'/../../components/com_virtuemart/virtuemart_parser.php' );
			} else {
				require_once( dirname(__FILE__).'/../components/com_virtuemart/virtuemart_parser.php' );
			}
			
			global $VM_LANG, $sess, $mm_action_url, $CURRENCY_DISPLAY;
			require_once( CLASSPATH . 'ps_product_attribute.php');
			$ps_product_attribute = new ps_product_attribute();
			
			$db = new ps_DB();
			require_once( CLASSPATH."ps_product.php" );
			$ps_product = new ps_product;
			
			if($item_limit > 0) {
				$item_limit = "LIMIT ".$item_limit;
			} else {
				$item_limit = "";
			}
			
			$q  = "SELECT DISTINCT product_sku, #__{vm}_product.product_id, product_name, product_s_desc, product_thumb_image FROM #__{vm}_product, #__{vm}_product_category_xref, #__{vm}_category WHERE \n";
		   
			$q .= "(#__{vm}_product.product_parent_id='' OR #__{vm}_product.product_parent_id='0') \n";
			
			$q .= "AND #__{vm}_product.product_id=#__{vm}_product_category_xref.product_id \n";
		   
			$q .= "AND #__{vm}_category.category_id=#__{vm}_product_category_xref.category_id \n";
			
			if( $catid != 0 ) {
				$q .= "AND #__{vm}_category.category_id='".$catid."' \n";
			}
			
			$q .= "AND #__{vm}_product.product_publish='Y' \n";
			
			if( $type=='featured' ) {
				$q .= "AND #__{vm}_product.product_special='Y' \n";
			}
				
			if( CHECK_STOCK && PSHOP_SHOW_OUT_OF_STOCK_PRODUCTS != "1") {
				$q .= " AND product_in_stock > 0 \n";
			}
			
			switch( $type ) {
				case 'random':
					$q .= "\n ORDER BY RAND() ".$item_limit;
					break;
				case 'newest':
					$q .= "\n ORDER BY #__{vm}_product.cdate DESC ".$item_limit;
					break;
				case 'oldest':
					$q .= "\n ORDER BY #__{vm}_product.cdate ASC ".$item_limit;
					break;
				default:
					$q .= "\n ORDER BY #__{vm}_product.cdate DESC ".$item_limit;
					break;
			}
			
			
			$db->query($q);
		 
			if( $db->num_rows() > 0 ) {
				$i = 0;
				$products = array();
				while($db->next_record()) {
					$flypage = $ps_product->get_flypage($db->f("product_id"));
				   
					$products[$i]['product_sku'] = $db->f("product_sku");
					$products[$i]['product_name'] = $db->f("product_name");
					
					$price = $ps_product->calcEndUserprice( $db->f("product_id"), false );
					$products[$i]['product_price'] = $CURRENCY_DISPLAY->getFullValue($price['product_price']);
					
					$short_desc =  strip_tags($db->f("product_s_desc"));
					if(strlen($short_desc) > $s_desc){
						$short_desc = substr($short_desc, 0, $s_desc).'...';
					}
					$products[$i]['product_s_desc'] = $short_desc;
					
					$url = "?page=shop.product_details&category_id=".$catid."&flypage=".$flypage;
					$url .= "&product_id=" . $db->f("product_id");
					$products[$i]['product_url'] = $sess->url('index.php'.$url, true);
					
					$products[$i]['product_thumb'] = $ps_product->image_tag( $db->f("product_thumb_image"), "class=\"slg-vm-productscroller-image\" alt=\"".$db->f("product_name")."\"");
					$products[$i]['product_id'] = $db->f("product_id");
					
					$i++;
				}
			 
				return $products;
			}
		 
			return '';
		}
		
		
		/**
		 * Make query to virtuemart table and get products by category ids
		 *
		 * @access	public
		 * @param	mixed $vm_category virtuemart category id or ids
		 * @param	integer $max_product query rows limit
		 * @param	string $product_type product type newest/feature etc.
		 * @return	mixed   an array of database objects using the current SQL query or false if the query fails
		 */
		function getProductsByCatIds($vm_category, $max_product, $product_type){
			$products = array();
			
			for($i=0; $i<count($vm_category); $i++){
				$products[$vm_category[$i]] = self::getProductByCatId($vm_category[$i], $max_product, $product_type);
			}
			
			return $products;
		}
	
	
	
		/**
		 * Configurable module css
		 *
		 * @access	public
		 * @param	array $cfg_array module parameters array
		 * @return	string module css
		 */
		function getModuleCSS($cfg_array){
			extract($cfg_array);
			
			$stylesheet = '<style type="text/css">';
			
			$stylesheet .= 'body { margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; }';
			
			$stylesheet .= '#slg-vm-module-scroller { width: '.$width.'px; margin: 0 auto; margin-bottom: 10px; }';
			
			$stylesheet .= '
				ul.slg-vm-tabs, 
				ul.slg-vm-buttons,
				ul.slg-vm-contents { list-style-type: none; padding: 0; margin: 0; }';
			
			$stylesheet .= '
				ul.slg-vm-tabs, 
				ul.slg-vm-contents { clear: left; width: 100000em; position: absolute; left: 0; top: 0; }';
			
			$stylesheet .= 'div.slg-vm-tabs-group { width: '.$tab_group_width.'px; height: '.$tab_height.'px; clear: left; overflow: hidden; position: relative; margin: 0 auto; }';
			
			$stylesheet .= 'div.slg-vm-button-group { width: '.$width.'px; clear: left; overflow: hidden; position: absolute; margin: 0 auto; }';
			
			$stylesheet .= 'ul.slg-vm-buttons li { display: inline; width:50px; cursor: pointer; z-index:1000; }';
			
			$stylesheet .= 'ul.slg-vm-tabs li { margin-left: '.$tab_item_margin_left.'px; margin-right: '.$tab_item_margin_right.'px; display: inline; float: left; cursor: default; z-index:1000; }';
			
			$stylesheet .= 'div.slg-vm-tab-contents-group { background:'.$background_color.' url('.$module_base_uri.'backgrounds/'.$background.') repeat left top; width: '.($width-($module_border*2)).'px; height: '.$content_height.'px; clear: left; overflow: hidden; position: relative; margin: 0; border:'.$module_border.' solid '.$module_border_color.'; }';
			
			$stylesheet .= 'ul.slg-vm-contents li { float: left; display: block; width: '.($width-40).'px; padding: 20px; }';
			
			$stylesheet .= 'table.slg-vm-tbl-product td { padding-left: 10px; padding-right: 10px;
							width: '; if($force_snapshot_width>0){$stylesheet .= $force_snapshot_width.'px';} else {$stylesheet .= (100/$max_product).'%';} $stylesheet .= ' !important; }';
							
			$stylesheet .= 'table.slg-vm-tbl-product td div{text-align:center !important;}';
			
			$stylesheet .= 'img.slg-vm-productscroller-image {border:'.$thmubs_border.' solid '.$thmubs_border_color.'; '; 
							if($force_thmubs_width>0){ $stylesheet .= 'width: '.$force_thmubs_width.'px;'; }
							if($force_thmubs_height>0){ $stylesheet .= 'height: '.$force_thmubs_height.'px;'; } $stylesheet .= '}';
			
			$stylesheet .= '</style>';
			
			$stylesheet .= '<link rel="stylesheet" type="text/css" href="'.$module_base_uri.'themes/'.$module_theme.'/style.css" />';
			
			return $stylesheet;
		}
		
		
		/**
		 * Configurable module js
		 *
		 * @access	public
		 * @param	array $cfg_array module parameters array
		 * @return	string module js
		 */
		function getModuleJS($cfg_array){
			extract($cfg_array);
			
			$js = '';
			
			if ($load_mootools) {
				$js .= '<script type="text/javascript" src="'.$module_base_uri.'assets/mootools.js"></script>';
			}
			
			$js .= '<script type="text/javascript" src="'.$module_base_uri.'assets/slg.vm.tabs.js"></script>';
			
			$js .= '<script type="text/javascript">';
			
			$js .= 'window.addEvent(\'domready\', loadSlgVmTabsScroller); ';
				
			$js .= '
					function loadSlgVmTabsScroller() {
						var tabs, contents;
			
						tabs = $$(\'.slg-vm-tabs\');
						contents = $$(\'.slg-vm-contents\');
			
						var tabView = new SlgVmTabs(tabs, contents, {
											activeClass: \'slg-vm-tab-active\',
											tabGroupWidth: '.$tab_group_width.',
											tabGroupSeparator: '.$tab_group_separator.',
											tabItemMarginLeft: '.$tab_item_margin_left.',
											tabItemMarginRight: '.$tab_item_margin_right.',
											duration: '.($slideshow_timeout/2).',
											autoPlay: '.$slideshow_autoplay.',
											autoPlayWait: '.$slideshow_timeout.'
										});
						';
				
			if($slideshow_nav_button){
				$js .= '
					$(\'slg-vm-tabs-next\').addEvent(\'click\', function() {
						tabView.loadSlgVmNextSlide();
					});
		
					$(\'slg-vm-tabs-prev\').addEvent(\'click\', function() {
						tabView.loadSlgVmPreviousSlide();
					});
				';
			}
			
			$js .= '$$(\'a\').addEvent(\'click\', function(){ if(!this.getProperty(\'href\').contains(\'#\')){ window.parent.location = this.getProperty(\'href\'); } else { return false; } });';
					
			$js .= '}';
			
			$js .= '</script>';
			
			return $js;
		}
		
		
		/**
		 * Module HTML content
		 *
		 * @access	public
		 * @param	array $cfg_array module parameters array
		 * @return	string module content
		 */
		function getLayoutHtml($cfg_array){
			extract($cfg_array);
			
			$html .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><base target="_parent" /><title>'.$module_name.'</title>';
			
			$html .= $module_css;
			
			$html .= $module_js;
			
			$html .= '</head><body>';
			
			$html .= '<div id="slg-vm-module-scroller">';
				
				/*-------------- START BUTTON GROUP --------------*/
				if($slideshow_nav_button) : 
				$html .= '<div class="slg-vm-button-group-wrapper"><div class="slg-vm-button-group"><ul class="slg-vm-buttons">
								<li><span id="slg-vm-tabs-prev">&nbsp;</span></li>
								<li><span id="slg-vm-tabs-next">&nbsp;</span></li>
						 </ul></div></div>';
				endif; 
				/*-------------- END BUTTON GROUP --------------*/
				
				
				
				/*-------------- START TAB GROUP --------------*/
				$html .= '<div class="slg-vm-tabs-group"><ul class="slg-vm-tabs">';
						   		foreach ($tab_title_list as $s){ $html .= '<li><a href="#slg_vm_scroller-'.$s->category_id.'"><span>'.stripslashes($s->category_name).'</span></a></li>'; }
				$html .= '</ul></div>';
				/*-------------- END TAB GROUP --------------*/
				
				
				/*-------------- START CONTENT GROUP --------------*/
				$html .= '<div class="slg-vm-tab-contents-group"><ul class="slg-vm-contents">';
						  foreach ($tab_content as $k=>$v){
								$html .= '<li id="slg_vm_slider-'.$k.'">';
								$html .= '<table class="slg-vm-tbl-product" border="0" cellspacing="0" cellpadding="0"><tr>';
									
								foreach($v as $p){
									$html .= '<td valign="top">';
										$html .= '<a href="'.$p['product_url'].'"><div>';
										$html .= '<div class="slg-vm-thumbs">'.$p['product_thumb'].'</div>';
											$html .= '<div class="slg-vm-s-desc">'.$p['product_s_desc'].'</div>';
											$html .= '<div class="slg-vm-price">'.$p['product_price'].'</div>';
										$html .= '</div></a>';
									$html .= '</td>';
								}
								
								$html .= '</tr></table>';
								$html .= '</li>';
							}
							
				$html .= '</ul></div>';	
				/*-------------- END CONTENT GROUP --------------*/
			$html .= '</div>';
			
			$html .= '</body></html>';
			
			return $html;
		}
	}	
} 